package org.dromara.device.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.dromara.common.core.constant.TenantConstants;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.device.domain.bo.DeviceConfigBo;
import org.dromara.device.domain.vo.DeviceConfigVo;
import org.dromara.device.service.IDeviceConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备管理
 * <p>
 * 顶级租户的超级管理人员才能有权限访问
 * </p>
 *
 * @author samuel
 * @date 2024-03-21 14:15:00
 */
@Log4j2
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("super/device")
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
public class SuperDeviceController extends BaseController {
    private final IDeviceConfigService deviceConfigService;

    /**
     * 查询设备管理-设备配置信息列表
     */
    @SaCheckPermission("device:config:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceConfigVo> list(DeviceConfigBo bo, PageQuery pageQuery) {
        return deviceConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设备管理-设备配置信息列表
     */
    @SaCheckPermission("device:config:export")
    @Log(title = "设备管理-设备配置信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DeviceConfigBo bo, HttpServletResponse response) {
        List<DeviceConfigVo> list = deviceConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备管理-设备配置信息", DeviceConfigVo.class, response);
    }

    /**
     * 获取设备管理-设备配置信息详细信息
     *
     * @param deviceId 主键
     */
    @SaCheckPermission("device:config:query")
    @GetMapping("/{deviceId}")
    public R<DeviceConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long deviceId) {
        return R.ok(deviceConfigService.queryById(deviceId));
    }

    /**
     * 新增设备管理-设备配置信息
     */
    @SaCheckPermission("device:config:add")
    @Log(title = "设备管理-设备配置信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceConfigBo bo) {
        return toAjax(deviceConfigService.insertByBo(bo));
    }

    /**
     * 修改设备管理-设备配置信息
     */
    @SaCheckPermission("device:config:edit")
    @Log(title = "设备管理-设备配置信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceConfigBo bo) {
        return toAjax(deviceConfigService.updateByBo(bo));
    }

    /**
     * 删除设备管理-设备配置信息
     *
     * @param deviceIds 主键串
     */
    @SaCheckPermission("device:config:remove")
    @Log(title = "设备管理-设备配置信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] deviceIds) {

        return toAjax(deviceConfigService.deleteWithValidByIds(List.of(deviceIds), true));
    }


}
